package com.starview.star.compiler.wff;

import com.starview.platform.data.type.Location;
import com.starview.star.compiler.CompilerUtils;
import com.starview.star.compiler.ast.IAbstract;
import com.starview.star.compiler.standard.StandardNames;
import com.starview.star.compiler.util.PrettyPrintDisplay;
import com.starview.star.compiler.wff.WffOp.applyMode;

@SuppressWarnings("serial")
public class WffTermPtn implements WffCond
{
  private final String category;
  private final int offset;

  public WffTermPtn(int offset, String category)
  {
    this.category = category;
    this.offset = offset;
  }

  @Override
  public applyMode satisfied(IAbstract env[], Location loc, WffEngine engine)
  {
    IAbstract term = env[offset];

    for (IAbstract el : CompilerUtils.unWrap(term)) {
      applyMode mode = engine.validate(el, category);

      if (mode != applyMode.validates)
        return mode;
    }
    return applyMode.validates;
  }

  @Override
  public void prettyPrint(PrettyPrintDisplay disp)
  {
    disp.append("?" + offset);
    disp.append(StandardNames.WFF_TERM);
    disp.append(category);
  }

  @Override
  public String toString()
  {
    return PrettyPrintDisplay.toString(this);
  }
}
